package Controle;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;



/**
 * Servlet implementation class FrontController
 */
@WebServlet("/FrontController")
public class FrontController extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FrontController() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String cod = request.getParameter("cod");
		try {
			if(cod.equals("0")){
				request.getRequestDispatcher("Protected/Base.jsp").forward(request, response);
			}else if(cod.equals("1")){
				request.getRequestDispatcher("Protected/ManterVeiculo.jsp").forward(request, response);
			}else if(cod.equals("2")){
				request.getRequestDispatcher("Protected/ManterCliente.jsp").forward(request, response);
			}else if(cod.equals("3")){
				request.getRequestDispatcher("Protected/Locacao.jsp").forward(request, response);
			}else if(cod.equals("4")){
				request.getRequestDispatcher("Protected/Devolucao.jsp").forward(request, response);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String operacao = request.getParameter("tipo");

		try {
			Command cmd = CommandFactory.obterCommand(operacao);
			String paginaDestino = cmd.executar(request, response);

			request.getRequestDispatcher(paginaDestino).forward(request,response);

		} catch (Exception e) {
			// tratamento de erro
			System.out.print(e.getStackTrace());
		}

	}

}
